var _addressContainer;
var _productList;
var _ordersTool;
var _payTool;
var _status;

var statusMap = {
    PAY: "待付款",
    DELIVERY: "待發貨",
    RECEIPT: "待收貨",
    EVALUATION: "待評價",
    CANCEL: "已取消",
    FINISH: "已完成"
}


function getProductItem(data) {
    var $productItem = $('<div class="product-item" ></div>');
    var $first = $('<div style="margin-bottom: .5rem"><img src="../../img/logo.png" style="height: .5rem; float: left"/><span style="font-size: .35rem; line-height: .5rem; display: block; float: left; margin-left: .15rem">傾城</span><div style="clear: both"></div></div>');
    var $second = $('<div style="margin-bottom: .5rem"><img src="' + data.coverPicture + '" style="height: 2.5rem; float: left"/><div style="margin-left: 2.85rem;"><div style="font-size: .3rem">' + data.name + '</div><div style="font-size: .3rem; color: #9b9b9b;">' + data.title + '</div><div style="margin-top: .2rem"><div style="float: left; color: red">NT$' + data.price + '</div><div style="float: right; font-size: .3rem"> x ' + data.num + '</div><div style="clear: both"></div></div></div></div>');

    $productItem.append($first, $second);
    return $productItem;
}

function init() {
    _status = $('.status');
    _addressContainer = $('.address-container');
    _productList = $('.product-list');
    _ordersTool = $('.orders-tool');
    _payTool = $('.pay-tool');
}

function initView() {

    var id = $.webUtil.getQueryString("id");
    var orderInfo;
    mui.ajax($.constant.webRoot() + 'v1/orders/vo/' + id, {
        dataType: 'json',
        type: 'GET',
        async: false,
        success: function (response) {
            if (response.code === 200) {
                orderInfo = response.data;
            }
        }
    });
    if (!orderInfo) {
        return;
    }

    _status.text(statusMap[orderInfo.status]);

    var address = orderInfo.address;
    _addressContainer.find('.name').text(address.receiveName);
    _addressContainer.find('.phone').text(address.receivePhone);
    _addressContainer.find('.address').text(address.receiveAddress);

    $.each(orderInfo.ordersItemVOList, function (i, item) {
        var productItem = getProductItem(item);
        _productList.append(productItem);
    });

    _ordersTool.find('.id').text(orderInfo.id);
    _ordersTool.find('.create-time').text(orderInfo.createTime);
    _ordersTool.find('.pay-method').text('貨到付款');
    _payTool.find('.product-price').text('NT$' + orderInfo.productPrice);
    _payTool.find('.postage-price').text('NT$' + orderInfo.postage);
    _payTool.find('.insurance-price').text('NT$' + orderInfo.insurance);
    _payTool.find('.cash-back-price').text('NT$' + orderInfo.cashBack);
    _payTool.find('.coupons-price').text('NT$' + orderInfo.coupons);
    _payTool.find('.total-price').text('NT$' + orderInfo.totalPrice);

}

$(function () {
    init();
    initView();
});